/*
 *  This file is part of Bracket Properties
 *  Copyright 2011-2013 David R. Smith
 *  All Rights Reserved
 *
 */
package asia.redact.bracket.util;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Singleton to generate a series of Integers. Can be reset.
 * 
 * @author Dave
 *
 */
public class SerialIDGenerator {

	private long id;
	private final Lock lock = new ReentrantLock();
	
	public static final SerialIDGenerator INSTANCE = new SerialIDGenerator();
	
	private SerialIDGenerator() {
		initialize(); // skip zero, we use that for the null token
	}
	
	public long increment() {
		try{
		lock.lock();
			long val = id;
			id++;
			return val;
		}finally{
			lock.unlock();
		}
	}
	
	public void initialize() {
		try{
			lock.lock();
			id=1;
		}finally{
			lock.unlock();
		}
	}
	
	public long currentValue() {
		return id;
	}

}
